package com.dbh.company.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.List;

public class ResizeImage {
    public static void main(String[] args) throws IOException {

        List<String> urls = FileUtil.readLines("D:\\data\\work\\file\\楼书\\楼书1.0\\楼书&数字人\\小区讲解图片.txt", Charset.defaultCharset());
        int count = 1;
        for (String url : urls) {
            byte[] bytes = HttpUtil.downloadBytes(url);
            InputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            BufferedImage resizedImage = resizeImage(byteArrayInputStream, 1920, 1080);
            ImageIO.write(resizedImage, "jpg", new File("D:\\data\\work\\file\\楼书\\楼书1.0\\楼书&数字人\\小区讲解\\" + count++ + ".jpg"));
        }

    }

    public static BufferedImage resizeImage(InputStream inputStream, int targetWidth, int targetHeight) throws IOException {
        BufferedImage sourceImage = ImageIO.read(inputStream);
        BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(sourceImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH), 0, 0, null);
        g.dispose();
        return resizedImage;
    }
}